<?php

class Admin_CueController extends GTL_Action {

    public function init() {
        parent::init();
        /* Initialize action controller here */
    }

    public function indexAction() {

        $questionData = Array();
        $showTemplate = false;
        $templateMessage = '';
        $hashTagname = '';
        $question_ids = '';
        $question_id_string = '';
        $url_id = '';

        //Check for the url id Parameter
        if ($this->_getParam('url_id') && $this->_getParam('url_id') != "") {

            $url_id = $this->_getParam('url_id');

            $urlModel = new Model_Url();


            $urlData = $urlModel->fetchUrls("url_lgn_id = '" . $this->USER->lgn_id . "' and url_id= '" . $url_id . "'");

            //Validation to check that a user can only view his meeting noth othr speakers meetings
            if ($urlData) {
                $urlStatus = $urlData['0']['url_status'];
                if (!$urlStatus) {
                    $showTemplate = false;
                    $templateMessage = "Sorry! This que is no more active";
                } else {
                    $showTemplate = true;
                    $hashTagname = $urlData['0']['url_hashtag'];
                    //Question Model
                    $questionModel = new Model_Question();
                    $questionData = $questionModel->fetchQuestions("q_url_id = '" . $url_id . "'");

                    if (count($questionData) > 0) {
                        foreach ($questionData as $_question) {
                            $question_id_string .= "" . $_question['q_id'] . ",";
                        }
                        $question_id_string = rtrim($question_id_string, ",");
                    }
                }
            } else {
                $showTemplate = false;
                $templateMessage = "Sorry! You donot have access to this meeting";
            }
        } else {
            $showTemplate = false;
            $templateMessage = "Sorry! There dosnot exist any such meeting";
        }

        $question_ids = "[" . $question_id_string . "]";
        
           
        
        $this->view->q_url_id = $url_id;
        $this->view->question_ids = $question_ids;
        $this->view->showTemplate = $showTemplate;
        $this->view->templateMessage = $templateMessage;
        $this->view->hashTagname = $hashTagname;
        $this->view->questionData = $questionData;
    }

    public function updatequestionAction() {

        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();

            $questionModel = new Model_Question();

            $questionData = $questionModel->fetchEntryById($postedData['q_id']);
            $question = '';
            if ($questionData) {
                $question = $questionData['q_question'];
            }

            $userModel = new Model_User();

            $where = "u_id = '" . $postedData['q_u_id'] . "'";
            $userData = $userModel->fetchUsers($where);

            if ($userData) {

                $email = $userData['0']['u_email'];

                $metaDataModel = new Model_Metadata();
                $ADMIN_MAIL_DATA = $metaDataModel->fetchEntryByKey("ADMIN_MAIL");
                $ADMIN_MAIL = $ADMIN_MAIL_DATA['mtd_value'];

                $body = "
				<html>
				<head>
				</head>
				<body>
				<table style='width:50%'>
				    <tr>
				    <td>
				    <p>Hello user </p>
				    <p>Please find answer to your question !</p>
				    <table border='1' cellpadding='10'>
					    <tr>
						    <td>Question</td>
						    <td align='center'>" . $question . "</td>
					    </tr>
					    <tr>
						    <td>Answer</td>
						    <td>" . $postedData['q_answer'] . "</td>
					    </tr>
				    </table>
				    <p>
				    Thanks,<br/>
				    The Admin Team
				    </p>
				     </td>
				    </tr>
				</table>
				</body>
				</html>
				";

                $mail = new Zend_Mail();
                $mail->setBodyText($body);
                $mail->setBodyHtml($body, "iso-8859-1");
                $mail->addTo($email, "User");
                $mail->setReplyTo($ADMIN_MAIL, 'Instant Q&A Admin team');
                $mailSubject = "Reply to question";
                $mail->addHeader('MIME-Version', '1.0');
                $mail->addHeader('Content-Transfer-Encoding', '8bit');
                $mail->addHeader('X-Mailer:', 'PHP/' . phpversion());
                $mail->setSubject($mailSubject);
                try {
                    $mail->send();
                } catch (Zend_Exception $e) {
                    
                }
            }

            $askData['q_id'] = $postedData['q_id'];
            $askData['q_answer'] = $postedData['q_answer'];
            $askData['q_answerddate'] = date("Y-m-d H:i:s");
            $questionModel->save($askData, 'q_id');
        }
        exit;
    }

}

?>
